package com.example.demo.controller;

import com.example.demo.command.PublishBookCmd;
import com.example.demo.command.UpdatePriceCmd;
import com.example.demo.entity.BookEntity;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Haibin Yuan
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping
    public BookEntity publishNewBook(@RequestBody PublishBookCmd command) {
        return bookService.publish(command);
    }

    @GetMapping("/all")
    public List<BookEntity> getAllBooks() {
        return bookService.findAllBooks();
    }

    @GetMapping
    public List<BookEntity> getBooksByAuthor(@RequestParam("author") String authorName) {
        return bookService.findByAuthor(authorName);
    }

    @GetMapping("/{id}")
    public BookEntity getBooksById(@PathVariable("id") Long id) {
        return bookService.findById(id);
    }

    @PutMapping
    public BookEntity updateBook(@RequestBody UpdatePriceCmd command) {
        return bookService.updatePrice(command);
    }

    @DeleteMapping("/{id}")
    public void destroyBook(@PathVariable("id") Long id) {
        bookService.destroy(id);
    }

}
